import { MigrationInterface, QueryRunner } from "typeorm"

// IDs of datasets not used by any chart and manually curated. The query to generate them runs in BigQuery
// and can be found here https://docs.google.com/spreadsheets/d/10pJoPIIFKeEKmvmk2rQ6Uwl6pUTIFoL9_stSy1Weo2M/edit?usp=sharing

const DATASETS_TO_ARCHIVE = [
    5417, 5365, 5219, 5158, 5156, 5112, 5086, 5087, 5084, 5082, 5068, 4871,
    4869, 4870, 4249, 4247, 4176, 4155, 4158, 4143, 4135, 3376, 3381, 3439,
    3447, 3452, 3472, 3473, 3528, 3529, 3530, 3531, 3532, 3533, 3534, 3535,
    3536, 3579, 3580, 3614, 3615, 3616, 3617, 3619, 3620, 3621, 3622, 3623,
    3624, 3625, 3626, 3627, 3628, 3629, 3630, 3633, 3649, 3655, 3694, 3697,
    3698, 3645, 3676, 3718, 3719, 3731, 3632, 3732, 3733, 3735, 3736, 3768,
    3859, 3897, 3898, 3899, 3900, 3901, 3902, 3903, 3904, 3905, 3906, 3907,
    3908, 3909, 3910, 3911, 3912, 3913, 3914, 3915, 3937, 3938, 3969, 3970,
    3972, 3973, 3974, 3975, 3976, 3977, 3981, 3982, 3983, 3994, 4008, 4026,
    4068, 3284, 3285, 3288, 3292, 3295, 3296, 3304, 3305, 3309, 3310, 3323,
    3324, 3327, 3335, 3336, 3337, 3341, 3346, 3268, 3267, 3073, 2858, 2787,
    2758, 2756, 2736, 2739, 2741, 2747, 2737, 2738, 2740, 2743, 2744, 2745,
    2746, 2748, 2742, 2704, 2676, 2677, 2678, 2679, 2680, 2681, 2682, 2683,
    2684, 2685, 2686, 2687, 2688, 2689, 2690, 2691, 2692, 2693, 2694, 2695,
    2696, 2697, 2525, 2526, 2527, 2528, 2531, 2532, 2533, 2536, 2537, 2538,
    2539, 2540, 2542, 2543, 2544, 2548, 2551, 2552, 2554, 2556, 2557, 2560,
    2561, 2562, 2567, 2570, 2572, 2575, 2576, 2577, 2578, 2580, 2581, 2583,
    2584, 2585, 2586, 2587, 2588, 2589, 2590, 2592, 2594, 2597, 2599, 2600,
    2601, 2602, 2603, 2605, 2606, 2607, 2608, 2611, 2614, 2615, 2616, 2625,
    2628, 2633, 2642, 2647, 2650, 2652, 2653, 2654, 2656, 2657, 2547, 2558,
    2559, 2564, 2565, 2574, 2582, 2598, 2609, 2610, 2618, 2619, 2620, 2621,
    2624, 2626, 2627, 2629, 2630, 2634, 2635, 2636, 2637, 2638, 2639, 2643,
    2645, 2646, 2648, 2649, 2651, 2655, 2658, 2604, 2530, 2534, 2535, 2563,
    2568, 2569, 2579, 2529, 2541, 2545, 2546, 2549, 2550, 2566, 2571, 2591,
    2595, 2596, 2612, 2613, 2617, 2622, 2623, 2631, 2632, 2640, 2641, 2644,
    2523, 2506, 2486, 2485, 2428, 2429, 2431, 2433, 2434, 2436, 2435, 2437,
    2438, 2440, 2441, 2432, 2442, 2443, 2444, 2445, 2446, 2447, 2401, 2402,
    2407, 2408, 2409, 2410, 2411, 2412, 2413, 2414, 2415, 2416, 2417, 2418,
    2419, 2420, 2421, 2422, 2424, 2078, 2079, 2080, 2081, 2082, 2083, 2084,
    2085, 2086, 2087, 2088, 2089, 2090, 2091, 2092, 2093, 2094, 2095, 2096,
    2097, 2098, 2099, 2100, 2101, 2102, 2103, 2104, 2105, 2106, 2107, 2108,
    2109, 2110, 2111, 2112, 2113, 2114, 2115, 2116, 2117, 2118, 2119, 2120,
    2121, 2122, 2123, 2124, 2125, 2126, 2127, 2128, 2129, 2130, 2131, 2132,
    2133, 2134, 2135, 2136, 2137, 2138, 2139, 2140, 2141, 2142, 2143, 2144,
    2145, 2146, 2147, 2148, 2149, 2150, 2151, 2152, 2153, 2154, 2155, 2156,
    2157, 2158, 2159, 2160, 2161, 2162, 2163, 2164, 2165, 2166, 2167, 2168,
    2169, 2170, 2171, 2172, 2173, 2174, 2175, 2176, 2177, 2178, 2179, 2180,
    2181, 2182, 2183, 2184, 2185, 2186, 2187, 2188, 2189, 2190, 2191, 2192,
    2193, 2194, 2195, 2196, 2197, 2198, 2199, 2200, 2201, 2202, 2203, 2204,
    2205, 2206, 2207, 2208, 2209, 2210, 2212, 2213, 2214, 2215, 2216, 2217,
    2218, 2219, 2220, 2221, 2222, 2223, 2224, 2225, 2226, 2227, 2228, 2229,
    2230, 2231, 2232, 2233, 2234, 2235, 2236, 2237, 2238, 2239, 2240, 2241,
    2242, 2243, 2244, 2245, 2246, 2247, 2248, 2249, 2250, 2251, 2253, 2254,
    2255, 2256, 2257, 2258, 2259, 2260, 2261, 2262, 2263, 2264, 2265, 2266,
    2267, 2268, 2269, 2270, 2271, 2272, 2273, 2274, 2275, 2276, 2277, 2278,
    2279, 2280, 2281, 2282, 2283, 2284, 2285, 2286, 2287, 2288, 2289, 2290,
    2291, 2292, 2293, 2294, 2295, 2296, 2297, 2298, 2299, 2300, 2301, 2302,
    2303, 2304, 2305, 2306, 2307, 2308, 2309, 2310, 2311, 2312, 2313, 2314,
    2315, 2316, 2317, 2318, 2319, 2320, 2321, 2322, 2323, 2324, 2325, 2326,
    2327, 2328, 2329, 2330, 2331, 2332, 2333, 2334, 2335, 2336, 2337, 2338,
    2339, 2340, 2341, 2342, 2343, 2344, 2345, 2346, 2347, 2348, 2349, 2350,
    2351, 2352, 2353, 2354, 2355, 2356, 2357, 2358, 2359, 2360, 2361, 2362,
    2363, 2364, 2365, 2366, 2367, 2368, 2369, 2370, 2371, 2372, 2373, 2374,
    2375, 2376, 2377, 2378, 2379, 2380, 2381, 2382, 2383, 2384, 2385, 2386,
    2387, 2388, 2389, 2390, 2391, 2392, 2393, 2394, 2395, 2396, 2397, 2399,
    2075, 2074, 1942, 1944, 1947, 1951, 1952, 1953, 1954, 1958, 1960, 1961,
    1962, 1963, 1964, 1972, 1975, 1976, 1977, 1980, 1984, 1986, 1988, 1991,
    1992, 1994, 1997, 2000, 2001, 2005, 2007, 2008, 2010, 2013, 2018, 2019,
    2023, 2024, 2027, 2029, 2030, 2032, 2033, 2036, 2046, 2056, 2057, 2058,
    2060, 2061, 2065, 2068, 2070, 2071, 2072, 2073, 2064, 2066, 1973, 1946,
    1955, 1970, 1985, 1989, 1990, 1995, 1999, 2002, 2003, 2016, 2035, 2038,
    2048, 2054, 2062, 1943, 1945, 1948, 1949, 1950, 1956, 1957, 1959, 1965,
    1966, 1967, 1968, 1969, 1971, 1974, 1978, 1979, 1981, 1982, 1983, 1987,
    1993, 1996, 1998, 2004, 2006, 2009, 2011, 2012, 2014, 2015, 2017, 2020,
    2021, 2022, 2025, 2026, 2028, 2031, 2034, 2037, 2039, 2040, 2041, 2042,
    2043, 2044, 2045, 2047, 2049, 2050, 2051, 2052, 2053, 2055, 2059, 2063,
    2067, 2069, 1938, 1898, 1884, 1863, 1864, 1868, 1856, 1867, 1869, 1865,
    1858, 1860, 1859, 1870, 1844, 1428, 1429, 1430, 1431, 1432, 1433, 1434,
    1435, 1436, 1437, 1438, 1439, 1440, 1441, 1442, 1443, 1444, 1445, 1446,
    1447, 1448, 1449, 1450, 1451, 1452, 1453, 1454, 1455, 1456, 1457, 1458,
    1459, 1460, 1461, 1462, 1463, 1464, 1465, 1466, 1467, 1468, 1470, 1471,
    1472, 1473, 1474, 1475, 1476, 1477, 1478, 1479, 1480, 1481, 1482, 1483,
    1484, 1485, 1486, 1487, 1488, 1489, 1490, 1491, 1492, 1493, 1494, 1495,
    1496, 1497, 1498, 1499, 1500, 1501, 1502, 1503, 1504, 1505, 1506, 1507,
    1508, 1509, 1510, 1511, 1512, 1513, 1514, 1515, 1516, 1517, 1518, 1519,
    1520, 1521, 1522, 1523, 1524, 1525, 1526, 1527, 1528, 1529, 1530, 1531,
    1532, 1533, 1534, 1535, 1536, 1537, 1538, 1539, 1540, 1541, 1542, 1543,
    1544, 1545, 1546, 1547, 1548, 1549, 1550, 1551, 1552, 1553, 1554, 1555,
    1556, 1557, 1558, 1559, 1560, 1561, 1562, 1563, 1564, 1565, 1566, 1567,
    1568, 1569, 1570, 1571, 1572, 1573, 1574, 1575, 1576, 1577, 1578, 1579,
    1580, 1581, 1582, 1583, 1584, 1585, 1586, 1587, 1588, 1590, 1591, 1592,
    1593, 1594, 1595, 1596, 1597, 1598, 1599, 1600, 1601, 1602, 1603, 1604,
    1605, 1606, 1607, 1608, 1609, 1610, 1611, 1612, 1613, 1614, 1615, 1616,
    1617, 1618, 1619, 1620, 1621, 1622, 1623, 1624, 1625, 1626, 1627, 1628,
    1629, 1630, 1631, 1632, 1633, 1634, 1635, 1636, 1637, 1638, 1639, 1640,
    1641, 1642, 1643, 1644, 1645, 1646, 1647, 1648, 1649, 1650, 1651, 1652,
    1653, 1654, 1655, 1656, 1657, 1658, 1659, 1660, 1661, 1662, 1663, 1664,
    1665, 1666, 1667, 1668, 1669, 1670, 1671, 1672, 1673, 1674, 1675, 1676,
    1677, 1678, 1679, 1680, 1681, 1682, 1683, 1684, 1685, 1686, 1687, 1688,
    1689, 1690, 1691, 1692, 1693, 1694, 1695, 1696, 1697, 1698, 1699, 1700,
    1701, 1702, 1703, 1704, 1705, 1706, 1707, 1708, 1709, 1710, 1711, 1712,
    1713, 1714, 1715, 1716, 1717, 1718, 1719, 1720, 1721, 1722, 1723, 1724,
    1725, 1726, 1727, 1728, 1729, 1730, 1731, 1732, 1733, 1734, 1735, 1736,
    1737, 1738, 1739, 1742, 1743, 1744, 1745, 1746, 1747, 1748, 1749, 1750,
    1751, 1752, 1753, 1754, 1755, 1756, 1757, 1758, 1759, 1760, 1761, 1762,
    1763, 1764, 1765, 1766, 1767, 1768, 1769, 1770, 1771, 1772, 1773, 1774,
    1775, 1776, 1777, 1778, 1779, 1780, 1781, 1782, 1783, 1784, 1785, 1786,
    1787, 1788, 1789, 1790, 1791, 1792, 1793, 1794, 1795, 1796, 1797, 1798,
    1799, 1800, 1801, 1802, 1803, 1804, 1805, 1806, 1807, 1808, 1809, 1810,
    1811, 1812, 1813, 1814, 1815, 1816, 1817, 1818, 1819, 1820, 1821, 1822,
    1823, 1824, 1825, 1826, 1827, 1828, 1829, 1830, 1045, 1048, 1058, 1060,
    1063, 1076, 1077, 1078, 1079, 1082, 1084, 1088, 1055, 1059, 1066, 1074,
    1075, 1090, 1081, 1044, 1054, 1064, 1073, 1042, 1043, 1046, 1050, 1057,
    1061, 1062, 1065, 1083, 1085, 1089, 1086, 1051, 1052, 1067, 1030, 973, 969,
    932, 927, 922, 923, 878, 880, 881, 886, 898, 899, 900, 905, 879, 887, 888,
    889, 890, 891, 892, 893, 894, 895, 896, 906, 909, 910, 912, 914, 915, 882,
    883, 884, 885, 903, 904, 907, 908, 911, 901, 902, 913, 897, 843, 776, 779,
    782, 784, 786, 787, 791, 793, 794, 795, 796, 799, 800, 801, 804, 806, 807,
    810, 813, 814, 815, 816, 817, 820, 821, 824, 825, 826, 827, 828, 831, 832,
    833, 835, 836, 837, 838, 839, 788, 798, 809, 818, 777, 790, 812, 823, 830,
    803, 802, 808, 819, 781, 811, 778, 783, 789, 840, 841, 797, 822, 834, 696,
    669, 645, 637, 618, 617, 616, 628, 630, 624, 627, 620, 626, 619, 621, 634,
    635, 636, 638, 593, 556, 557, 558, 559, 417, 2997, 350, 306, 280, 207, 2980,
    153, 137, 2979,
]

export class ArchiveDatasets1653646292637 implements MigrationInterface {
    public async up(queryRunner: QueryRunner): Promise<void> {
        await archiveDatasets(queryRunner, DATASETS_TO_ARCHIVE)
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await archiveDatasets(queryRunner, DATASETS_TO_ARCHIVE, true)
    }
}

const archiveDatasets = async (
    queryRunner: QueryRunner,
    datasetIds: number[],
    revert: boolean = false
): Promise<void> => {
    // label datasets as archived in DB
    await queryRunner.query(
        `UPDATE datasets SET isArchived = ${revert ? 0 : 1}
        WHERE id in (?)`,
        [datasetIds]
    )
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const deleteDatasets = async (
    queryRunner: QueryRunner,
    datasetIds: number[]
): Promise<void> => {
    // delete datasets from DB, not used at the moment
    await queryRunner.query(
        `
  DELETE from data_values WHERE variableId in (
      SELECT id from variables WHERE datasetId in (?)
  )`,
        [datasetIds]
    )
    await queryRunner.query(
        `
  DELETE from country_latest_data WHERE variable_id in (
      SELECT id from variables WHERE datasetId in (?)
    )`,
        [datasetIds]
    )

    await queryRunner.query(
        `
  DELETE from chart_dimensions WHERE variableId in (
    SELECT id from variables WHERE datasetId in (?)
  )`,
        [datasetIds]
    )

    await queryRunner.query(`DELETE from variables WHERE datasetId in (?)`, [
        datasetIds,
    ])
    await queryRunner.query(`DELETE from sources WHERE datasetId in (?)`, [
        datasetIds,
    ])
    await queryRunner.query(`DELETE from datasets WHERE id in (?)`, [
        datasetIds,
    ])
}
